﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.Data.SqlClient;
using System.Collections;
using Entity;

namespace Web.admin.BasicManage.students
{
    public partial class STU_SelList : System.Web.UI.Page
    {
        StudentService studentservice = new StudentService();
        private static string sqlcon;
        private static string searchCon;
        private static string seachTxt;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["searchCon"]) && !string.IsNullOrEmpty(Request.QueryString["searchTxt"]))
                {
                    searchCon = Request.QueryString["searchCon"];
                    seachTxt = Request.QueryString["searchTxt"];
                    sqlcon = " and " + searchCon+ " ='" + seachTxt + "'";
                    RpListBind(sqlcon);
                }
            }
        }
        /// <summary>
        /// 根据条件对数据表进行绑定
        /// </summary>
        /// <param name="sqlcon">sql条件</param>
        protected void RpListBind(object sqlcon)
        {
            RpList.DataSource = studentservice.GetAllStudents(sqlcon);
            RpList.DataBind();
        }

        /// <summary>
        /// 排序列表绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlorderChange(object sender, EventArgs e)
        {
            string sqlorder = " order by " + ddlorder.SelectedValue;
            RpListBind(sqlcon+sqlorder);
        }

        /// <summary>
        /// 批量删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BatchDelete(object sender, EventArgs e)
        {
            ArrayList StudentsId = new ArrayList();
            for (int i = 0; i < RpList.Items.Count; i++)
            {
                string id = ((HiddenField)RpList.Items[i].FindControl("hf_StudentId")).Value;
                CheckBox cb = (CheckBox)RpList.Items[i].FindControl("CbChoose");
                if (cb.Checked)
                {
                    StudentsId.Add(id);
                }
            }
            int result = studentservice.DleteMoreStudents(StudentsId);
            Response.Write("<script>alert('删除成功，共删除" + result + "条数据');window.location.href='STU_SelList.aspx?searchCon=" + searchCon + "&searchTxt=" + seachTxt+ "'; </script>");
            return;
        }

        /// <summary>
        /// 添加学生事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AddStudent(object sender, EventArgs e)
        {
            BoStudents bostudents = new BoStudents();
            bostudents.STU_CLASSES = ddl_Class_add.SelectedValue;
            bostudents.STU_CODE = txt_studentCode_add.Value;
            bostudents.STU_NAME = txt_studentName_add.Value;
            bostudents.STU_USERNAME = txt_studentUsm_add.Value;
            bostudents.STU_PASSWORD = txt_studentPwd_add.Value;
            if (rd_female_add.Checked)
                bostudents.STU_GENDER = "0";
            else if (rd_male_add.Checked)
                bostudents.STU_GENDER = "1";
            if (!string.IsNullOrEmpty(txt_studentGrade_add.Value))
                bostudents.STU_GRADE = txt_studentGrade_add.Value;
            else
            {
                Response.Write("<script>alert('输入的年级有误，请检查格式')</script>");
                return;
            }
            studentservice.CreateStudent(bostudents);
            Response.Write("<script>alert('添加成功');window.location.href='STU_SelList.aspx?searchCon=" + searchCon + "&searchTxt=" + seachTxt + "'; </script>");
        }

        /// <summary>
        /// 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Rp_Event(object sender, RepeaterCommandEventArgs e)
        {
            string stu_id = ((HiddenField)e.Item.FindControl("hf_StudentId")).Value;
            BoStudents studentsmodel = new BoStudents();
            studentsmodel.STU_ID = stu_id;
                switch (e.CommandName)
                {
                    case "stu_Del":
                        studentservice.RemoveStudents(studentsmodel);
                        break;
                }
                RpListBind(sqlcon);
        }
        /// <summary>
        /// 修改学生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void EditStudent(object sender, EventArgs e)
        {
            BoStudents studentModel = new BoStudents();
            studentModel.STU_CLASSES = ddl_students_edit.SelectedValue;
            studentModel.STU_CODE = txt_studentCode_edit.Value;
            if (rd_male_edit.Checked)
                studentModel.STU_GENDER = "1";
            else if (rd_female_edit.Checked)
                studentModel.STU_GENDER = "0";
            if (!string.IsNullOrEmpty(txt_studentGrade_edit.Value))
                studentModel.STU_GRADE = txt_studentGrade_edit.Value;
            studentModel.STU_ID = stu_Id.Value;
            studentModel.STU_NAME = txt_studentName_edit.Value;
            studentModel.STU_PASSWORD = txt_studentPwd_edit.Value;
            studentModel.STU_USERNAME = txt_studentUsm_edit.Value;
            studentservice.UpdateStudent(studentModel);
            Response.Write("<script>alert('修改成功');window.location.href='STU_SelList.aspx?searchCon=" + searchCon + "&searchTxt=" + seachTxt + "';</script>");
        }
        
    }
}